<?php

namespace app\model\system;

use app\base\BaseModel;
use app\libs\Oss;
use think\model\concern\SoftDelete;

/**
 * 模型-banner
 * Class BannerModel
 * @package app\model
 */
class BannerModel extends BaseModel
{
    use SoftDelete;

    protected $name = 'banner';

    /**
     * 删除redis
     */
    public function delAllRedis()
    {
        $this->redis->del($this->redis_key);
        $this->redis->del($this->redis_key . ':rank');
    }

    /**
     * 彻底删除
     * @param $id
     * @return bool
     * @throws \Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delComplete($id)
    {
        $banner = self::onlyTrashed()->find($id);
        Oss::delOssFile($banner['banner_src']);
        return $banner->force()->delete();
    }

    /**
     * 还原数据
     * @param $id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function backData($id)
    {
        $banner = self::onlyTrashed()->find($id);
        $this->setRedis($banner->toArray());
        if ($banner['status'] == 1) {
            $this->redis->zAdd($this->redis_key . ':rank', $banner['rank'], $banner['id']);
        }
        return $banner->restore();
    }

    /**
     * 范围查询-status
     * @param $query
     * @param $status
     */
    public function scopeStatus($query, $status)
    {
        if ($status != null) {
            $query->where('status', '=', $status);
        }
    }

    /**
     * 范围查询-jump_type
     * @param $query
     * @param $jump_type
     */
    public function scopeJumptype($query, $jump_type)
    {
        if ($jump_type != null) {
            $query->where('jump_type', '=', $jump_type);
        }
    }

    /**
     * 范围查询-keyword
     * @param $query
     * @param $keyword
     */
    public function scopeKeyword($query, $keyword)
    {
        if (!empty($keyword)) {
            $query->whereLike('banner_title', '%' . trim($keyword) . '%');
        }
    }

    /**
     * 范围查询-Area
     * @param $query
     * @param $area
     */
    public function scopeArea($query, $area)
    {
        if (!empty($area)) {
            if ($area == 'all') {
                $query->where('area', '<>', 'index');
            } else {
                $query->where('area', $area);
            }

        }
    }

    /**
     * 获取分页列表
     * @param $status
     * @param $jump_type
     * @param $keyword
     * @param $current_page
     * @param $list_rows
     * @param string $field
     * @param string $area
     * @return array
     */
    public function getPageData($status, $jump_type, $keyword, $current_page, $list_rows, $field = '*', $area = 'index')
    {
        $dataList = $this
            ->area($area)
            ->field($field)
            ->status($status)
            ->jumptype($jump_type)
            ->keyword($keyword)
            ->order('rank', 'desc')
            ->paginate(['page' => $current_page, 'list_rows' => $list_rows], false);

        $current = $dataList->currentPage();
        $total = $dataList->total();
        $limit = $dataList->listRows();
        $ret = ['total' => $total, 'current' => $current, 'limit' => $limit, 'dataList' => $dataList->items()];
        return $ret;
    }

    /**
     * 获取不存在id的其它分页数据
     *
     * @param [type] $status
     * @param [type] $jump_type
     * @param [type] $keyword
     * @param [type] $current_page
     * @param [type] $list_rows
     * @param string $field
     * @param string $area
     * @param [type] $idArr
     * @return void
     */
    public function getPageDataByNotInId($status, $jump_type, $keyword, $current_page, $list_rows, $field = '*', $area = 'index',$idArr)
    {
        $dataList = $this
            ->area($area)
            ->field($field)
            ->status($status)
            ->jumptype($jump_type)
            ->keyword($keyword)
            ->order('rank', 'desc')
            ->where('id','not in',$idArr)
            ->paginate(['page' => $current_page, 'list_rows' => $list_rows], false);

        $current = $dataList->currentPage();
        $total = $dataList->total();
        $limit = $dataList->listRows();
        $ret = ['total' => $total, 'current' => $current, 'limit' => $limit, 'dataList' => $dataList->items()];
        return $ret;
    }
}
